<template>
  <div>
    <div v-if="assetsList.length" class="container-box">
      <h3 class="title">現有追蹤器</h3>
      <el-descriptions class="margin-top" title="" :column="5" size="medium" border v-for="(item, index) in assetsList" :key="index">
        <el-descriptions-item>
          <template slot="label">
            設備名稱：
          </template>
          {{ item.facilityName }}
        </el-descriptions-item>
        <el-descriptions-item contentClassName="hidden-text">
          <template slot="label">
            IMEI：
          </template>
          {{ item.facilityNum }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            設備狀態：
          </template>
          <span :class="[ item.facilityState === 0 ? 'green' : 'red']">{{ item.facilityState === 0 ? '未使用' : '使用中' }}</span>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            開機狀態：
          </template>
          <span :class="[ item.isOnline === 1 ? 'green' : 'red']">{{ item.isOnline === 1 ? '開機' : '關閉' }}</span>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            操作：
          </template>
          <el-button type="danger" @click="onDelFacility(item.facilityId)" size="small">刪除</el-button>
        </el-descriptions-item>
      </el-descriptions>
    </div>
    <el-empty v-else description="暫無數據"></el-empty>
    <el-row :gutter="10" justify="end" type="flex" class="text-right">
      <el-col :xs="12" :sm="12" :md="6" :lg="3" :xl="2"><el-button type="primary" @click="drawer = true">添加追蹤器</el-button></el-col>
    </el-row>
    <el-drawer
      title="添加設備"
      :visible.sync="drawer"
      direction="btt"
      :wrapperClosable="false"
      ref="drawer"
      >
      <el-row :gutter="10" justify="end" type="flex" class="text-right">
        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
          <el-input placeholder="請輸入設備名稱" v-model="form.facilityName">
            <template slot="prepend">名称</template>
          </el-input>
        </el-col>
        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
          <el-input placeholder="請輸入IMEI" v-model="form.facilityNum">
            <template slot="prepend">IMEI</template>
          </el-input>
        </el-col>
        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
          <el-input placeholder="請輸入設備密碼" v-model="form.password" type="password">
            <template slot="prepend">Password</template>
          </el-input>
        </el-col>

      </el-row>
      <el-row :gutter="10" justify="space-between" type="flex" class="text-right" style="margin-top: 10px;">
        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
          <el-input placeholder="請輸入私鑰" v-model="form.privateKey" type="text">
            <template slot="prepend">私鑰</template>
          </el-input>
        </el-col>
        <el-col :xs="12" :sm="12" :md="6" :lg="6" :xl="6">
          <el-button type="primary" @click="onAddAssets">確定</el-button>
        </el-col>
      </el-row>
    </el-drawer>
  </div>

</template>

<script>
export default {
  name: 'assets-index',

  data() {
    return {
      assetsList: [],
      ruleForm: {
        userId: ''
      },
      loading: false,
      drawer: false,
      form: {
        facilityName: '',
        facilityNum: '',
        password: '',
        privateKey: ''
      }
    }
  },

  created() {
    let user = sessionStorage.getItem('user')
    if (user) {
      user = JSON.parse(user)
      this.ruleForm.userId = user.id || ''
    }
  },

  mounted() {
    this.getData()
  },

  methods: {
    //移除设备
    onDelFacility(id) {
      this.$confirm('此操作將移除id為'+id+'的追蹤器, 是否繼續?', '提示', {
        confirmButtonText: '確定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        this.$store.dispatch('assets/fetchDelFacility', { facilityId: id }).then(res => {
          // console.log('移除追踪器')
          // console.log(res)
          this.loading = false
          this.$message.success(res.message)
          this.getData()
        }).catch(err => {
          this.loading = false
          this.$message.error(err)
          return false
        })
      }).catch(() => {
        //取消动作
      })
    },

    //获取追踪器列表
    getData() {
      this.loading = true
      this.$store.dispatch('assets/fetchListUserFacility').then(res => {
        // console.log('获取追踪器列表')
        // console.log(res)
        this.assetsList = res
        this.loading = false
        // this.$message.success(res.message)
      }).catch(err => {
        this.loading = false
        this.$message.error(err)
        return false
      })
    },

    //添加设备-确认按钮
    onAddAssets() {
      if(!this.form.facilityName) {
        this.$message.error('設備名稱不能為空')
        return false
      }
      if(!this.form.facilityNum) {
        this.$message.error('IMEI不能為空')
        return false
      }
      if(!this.form.password) {
        this.$message.error('password不能為空')
        return false
      }
      if(!this.form.privateKey) {
        this.$message.error('私鑰不能為空')
        return false
      }
      this.loading = true
      this.$store.dispatch('assets/fetchCheckFacility', this.form).then(res => {
        this.$store.dispatch('assets/fetchAddFacility', { ...this.form, facilityId: res.result.facilityId }).then(res => {
          // console.log('添加设备')
          // console.log(res)
          this.loading = false
          this.$message.success(res.message)
          //关闭抽屉
          this.$refs.drawer.closeDrawer()
          this.getData()
          this.form = {
            facilityName: '',
            facilityNum: '',
            password: '',
            privateKey: ''
          }
        }).catch(err => {
          this.loading = false
          this.$message.error('添加设备：' + err)
          return false
        })
      }).catch(err => {
        this.loading = false
        this.$message.error('设备检查：' + err)
        return false
      })
    },
  }
}
</script>

<style scoped lang="scss">
@import "../../assets/css/them.scss";

.container-box {
  padding: 10px 20px;
}

/deep/ .el-descriptions .is-bordered {
  table-layout: fixed !important;
}

.green {
  color: $connectColor;
}

.red {
  color: $disconnectColor;
}

.text-right {
  padding: 0 20px;
  text-align: right;
}

/deep/ .hidden-text {
  overflow: hidden;  /*溢出隐藏*/
  text-overflow: ellipsis; /*以省略号...显示*/
  // white-space: nowrap;  /*强制不换行*/
  word-break:break-all;
}

.margin-top {
  margin-top: 10px;
}
</style>
